package com.atguigu.guli.service.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.atguigu.guli.service.vod.service.VodService;
import com.atguigu.guli.service.vod.utils.ConstantVodUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadVideo(MultipartFile file) throws IOException {
        //title :阿里云显示名称
        //filename:上传文件原始名称
        //inputStream 上传文件流
        String title = "";
        String filename = file.getOriginalFilename();
        String substring = filename.substring(0, filename.lastIndexOf("."));
        title = substring;
        InputStream inputStream = file.getInputStream();

        UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESS_KEY_ID,ConstantVodUtils.ACCESS_KEY_SECRET,title,filename,inputStream);
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        String videoId =null;
        if (response.isSuccess()){
              videoId = response.getVideoId();
        }else {
            videoId = response.getVideoId();
        }
        return videoId;
    }
}
